package com.design.patterns.simplefactory;

import com.design.enums.PayModeEnum;

import javax.crypto.KeyGenerator;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jack
 * @version 1.0
 * @Package com.design.patterns.simplefactory
 * @Description:
 * @date 2021/6/6  17:23
 */
public class PayModeFactory {

    /**
     * 根据类型获取支付方式
     * @param type
     * @return
     */
    public static AbstractPay getPayMode(String type) {
        List<PayModeEnum> eumValueList = PayModeEnum.getEumValueList();
        List<String> payModeList = eumValueList.stream().map(s -> s.getCode()).collect(Collectors.toList());
        if(!payModeList.contains(type)){
           throw  new RuntimeException("暂不支持该支付方式");
        }
        if (PayModeEnum.CARD_PAY.getCode().equals(type)) {
            return new CardPay();
        }
        if (PayModeEnum.WEIXIN_PAY.getCode().equals(type)) {
            return new WeixinPay();
        }
        if (PayModeEnum.ALIPAY_PAY.getCode().equals(type)) {
            return new AlipayPay();
        }
        return null;
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        AbstractPay payMode = getPayMode("weixin");
        if(payMode!=null){
            payMode.pay();
        }
    }
}
